package com.homwee.factory.pageData;

import java.util.List;

import android.content.ComponentName;
import android.content.Intent;
import android.util.Log;

import com.homwee.factory.FactoryActivity;
import com.homwee.factory.R;
import com.homwee.test.CHFactoryTest;
import com.homwee.listMenu.util.MenuItemData;
import android.app.ActivityManager;
import android.content.Context;
//import com.changhong.tvos.common.MiscManager;
//import com.changhong.tvos.common.TVManager;
//import com.changhong.tvos.common.exception.IllegalValueException;
//import com.changhong.tvos.common.exception.TVManagerNotInitException;

public class PageData4 extends PageData{
	
	public PageData4(FactoryActivity mFactoryActivity){
		super(mFactoryActivity);
	}
	MenuItemData volumeControl;
	MenuItemData voiceBalance;
	MenuItemData micFacDetect;
	MenuItemData Inspection;
	MenuItemData selfCheck;
	public List<MenuItemData> LoadPageData(){		
		mListItemData.clear();
		String volumeControlStr = mFactoryActivity.getResources().getString(R.string.factory_volume_control);
		volumeControl =new MenuItemData(0,volumeControlStr,0,2,1,"",1);
		volumeControl.isCycle =false;
		try {
			volumeControl.mOptionStr ="                      "+factorySetting.getVolume();
			if(factorySetting.getVolume() <50){
				volumeControl.mInitValue =0;
			}else{
				volumeControl.mInitValue =2;
			}
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		volumeControl.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setVolume");
				try {
					factorySetting.setVolume(value*50);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				volumeControl.updateValue("                      "+value*50);
			}
		});

		String []voiceBalanceArray={"-50","0","50"};
		try {
			factorySetting.setBalance(0);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String voiceBalanceStr = mFactoryActivity.getResources().getString(R.string.factory_voice_balance);
		voiceBalance =new MenuItemData(0,voiceBalanceStr,0,2,1,voiceBalanceArray,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		voiceBalance.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setBalance");
				try {
					factorySetting.setBalance(-50+value*50);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		String InspectionStr = "Inspection switch";
		Inspection = new MenuItemData(0,InspectionStr,0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		Inspection.mInitValue = IsInspectionDebugOn()?1:0;//factorySetting.getAgingModel();
		/*
		Inspection.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			@Override
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				try {
				Intent intentForInspection = new Intent(mContext,com.homwee.serialportinspection.InspectionService.class);
					mContext.startService(intentForInspection);
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
		});
		*/
		Inspection.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setInspection");
				if(value == 1){
					try {
						Intent intentForInspection = new Intent(mContext,com.homwee.serialportinspection.InspectionService.class);
						intentForInspection.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						mContext.startForegroundService(intentForInspection);
					} catch (Exception e) {
						// TODO: handle exception
					}
				} else {
					Intent stopInspection = new Intent();
					stopInspection.setAction("com.homwee.serialportinspection.action.STOP");
					mContext.sendBroadcast(stopInspection);

				}
			}
		});	
		
                //chuankou  
		String micFacDetectStr = mFactoryActivity.getResources().getString(R.string.factory_voice_micdetect);
		micFacDetect =new MenuItemData(micFacDetectStr);
		micFacDetect.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			@Override
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				try {
		    		Intent mIntent = new Intent();
					mIntent.setComponent(new ComponentName("com.changhong.system.voice","com.changhong.system.voice.mic.facdetect.MicFacDetectActivity"));
					mIntent.addFlags(mIntent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(mIntent);
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
		});

		selfCheck =new MenuItemData("Self-Check");
		selfCheck.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			@Override
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				try {
				Log.v("tv","selfCheck in");
					CHFactoryTest test = new CHFactoryTest(mContext);
					test.checkPanelUpdate();
					test.checkPQUpdate();
					test.checkAQUpdate();
					Intent intentAutoTestRetShow = new Intent();
					intentAutoTestRetShow.setClassName("com.homwee.factory","com.homwee.factory.CHFactoryCheckActivity");                                                                                                                       
					mContext.startActivity(intentAutoTestRetShow);
				Log.v("tv","selfCheck out");
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
		});
		mListItemData.add(volumeControl);
		mListItemData.add(voiceBalance);
		mListItemData.add(Inspection);
		mListItemData.add(selfCheck);
		
		return mListItemData;
	}

	private boolean IsInspectionDebugOn() {
		boolean isRunning = false;
		ActivityManager activityManager = (ActivityManager) mContext
			.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> serviceList = activityManager
			.getRunningServices(30);

		if (!(serviceList.size() > 0)) {
			return false;
		}

		for (int i = 0; i < serviceList.size(); i++) {
			if (serviceList.get(i).service.getClassName().equals(
						"com.homwee.serialportinspection.InspectionService") == true) {
				isRunning = true;
				break;
						}
		}
		return isRunning;
	}

}
